package com.four.basicinfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.four.basicinfo.dao.UserDao;
import com.four.basicinfo.entity.User;
import com.four.basicinfo.service.UserService;
import com.four.basicinfo.until.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {





    @Autowired
    private UserDao userDao;

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public User login(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        String pass= Md5Util.getMD5String(password);
        queryWrapper.eq( "username",username ).eq( "password",pass );
        User user = userDao.selectOne( queryWrapper );
        if(user!=null) {
            if (user.getIslock() == "锁定") {
                return null;
            }
        }
        return user;
    }

    @Override
    public Boolean adduser(User user) {
        if (user.getUsername()==null){
            return false;
        }
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper=queryWrapper.eq( User::getUsername,user.getUsername() );
        User user1 = userDao.selectOne( queryWrapper );
        if (user1!=null){
            return false;
        }
        user.setPassword("abc123");
        user.setIslock( "正常" );
        String pas= Md5Util.getMD5String(user.getPassword());
        user.setPassword(pas);
        userDao.insert( user );

        return true;

    }

    @Override
    public List<User> getUsers(User user) {
        QueryWrapper wrapper = new QueryWrapper();

        if (null != user) {
            if (user.getName() != null) {
                wrapper.like("name", user.getName());
            }
            if (user.getUsername() != null) {
                wrapper.like("username", user.getUsername());
            }
            if (user.getRole() != null) {
                wrapper.like("role", user.getRole());
            }
            if (user.getIslock() != null) {
                wrapper.like("islock", user.getIslock());
            }
        }
        return  userDao.selectList(null);

    }

    @Override
    public Boolean updateById(Integer id,String role,String islock) {
        // 数据校验
        if(id==null) {
            return false ;
        }
        User user = new User() ;
        user.setId(id);
        if(role!=null) {
            user.setRole(role);
        }
        if(islock!=null) {
            user.setIslock(islock);
        }
        return userDao.updateById(user) > 0;
    }

    @Override
    public Boolean updatepass(String username, String password) {
        String pas= Md5Util.getMD5String(password);
        return userDao.updatepass(pas,username) > 0;
    }


    /**
     * 分页查询
     * @return
     */
   /* @Override
    public PageResult getUsers(int pageNo, int pageSize) {
        Page<User> page = new Page<>(pageNo, pageSize);
        // 示例查询条件，可以根据实际需求修改
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq( "role","common" );
        userDao.selectPage(page, queryWrapper);
        List<User> userList = page.getRecords();
        long total = userList.size();
        System.out.println(total);

        // 构建返回结果
        PageResult pageResult = new PageResult();
        pageResult.setRecords(userList);
        pageResult.setTotal(total);

        return pageResult;
    }*/

    @Override
    public Boolean delById(Integer id) {
        int i = userDao.deleteById( id );
        if (i>=1){
            return true;
        }else {
            return false;
        }
    }

}
